package com.stars.empoly.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.stars.empoly.R
import com.stars.empoly.adapter.BillAdapter
import com.stars.empoly.base.MBRFragment
import com.stars.empoly.config.MIntentAction
import com.stars.empoly.databinding.FragmentAccountBillBinding
import com.stars.empoly.databinding.ItemAccountHeadBinding
import com.stars.empoly.model.CompreBean
import com.stars.empoly.vmodel.AccountBillVModel
import com.trailer.base.net.IResp

class AccountBillFragment : MBRFragment<AccountBillVModel, BillAdapter, FragmentAccountBillBinding>() {

    var headBinding: ItemAccountHeadBinding? =null
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        setHead()
        vm.fragment = this
        vm.getAmountCount()

        vm.info.observe(this, Observer {
            if (null == it) {
                return@Observer
            }
            headBinding!!.vm = vm
        })
    }

    private fun setHead() {
        //套餐详情
        //val headerView: View = layoutInflater.inflate(R.layout.item_account_head, null)
         headBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_account_head, null, false)
        val headerView: View = headBinding!!.root
        adapter().setHeaderView(headerView)
    }


    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)

        if (null != res) {

            var list = res.data() as List<CompreBean>
            if (list.isEmpty() && vm.repository().page == 1) {

                binding.listEmpty.visibility = View.VISIBLE
            } else {
                binding.listEmpty.visibility = View.GONE
            }

        }
    }

    override fun initAdapter(): BillAdapter {
        return BillAdapter()
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentAccountBillBinding {
        return FragmentAccountBillBinding.inflate(layoutInflater, container, false)
    }

    override fun title(): String? {
        return null
    }

    override fun vmClass(): Class<AccountBillVModel> {
        return AccountBillVModel::class.java
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (MIntentAction.REQUEST_CODE_ONE == requestCode && resultCode == Activity.RESULT_OK) {
            //刷新
            vm.getAmountCount()
            onRefresh()
        }
    }

    override fun onRefresh() {
        super.onRefresh()
        vm.getAmountCount()
    }
}
